require 'active_record'
require 'yaml'
require 'logger'
require 'sqlite3'
require "rails/generators"

task :default => :migrate

task :migrate => :development do
  ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
end

task :development do
  ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml')))
  ActiveRecord::Base.logger = Logger.new(File.open('db/database.log', 'a'))
end

desc 'create migration' 
task :new_migration, [:name, :options] do |t, args|
	name = args[:name]
	options = args[:name]
	generator_params = [name] + options.split(" ")
	Rails::Generators.invoke "active_record:migration", generator_params

	exit
	#name = args[:name]
	#time = Time.new.strftime("%Y%m%d%H%M%S")
	#new_file = "db/migrate/#{time}_#{name}.rb"
	#cp "db/new_migration", new_file

	#替换索引文字
	#text = IO.read(new_file)
	#text = text.gsub("{class}", name)
	#IO.write(new_file, text)
end

